const ArticleModel = require("../model/article")
const UsersModel = require('../model/users')


exports.addArticle = async (req, res, next) => {
    let { title, content}=req.body
    console.log(req.user);
    const user_id = req.user.id
    ArticleModel.addArticle({title,content,user_id}).then(result=>{
            return res.send({
                code: 0,
                data: result,
                msg: "添加成功"
            })
        })
}


exports.updateArticle = async (req, res, next) => {
  const {id} =req.params
  ArticleModel.updateArticle(id,req.body).then(result=>{
    return res.send({
        code:0,
        msg:"操作成功",
        data:result
    })
  })
}


exports.deleteArticle = async (req, res, next) => {
    const {id} =req.params
    ArticleModel.delArticle(id).then(result=>{
        return res.send({
            code:0,
            msg:"删除成功",
            data:result
        })
    })
}

exports.getAllArticle = async (req, res, next) => {
    let { currentPage, pageSize } = req.query
    ArticleModel.findAll({
        limit: parseInt(pageSize),
        offset: (parseInt(currentPage) - 1) * parseInt(pageSize)
    }).then((result) => {
        return res.send({
            code: 0,
            data: result,
            msg: "操作成功"
        })
    })
}

exports.getArticleById = async (req, res, next) => {
    const {id:article_id} =req.params
    ArticleModel.findByPk(article_id,{
        include:[{
            model: UsersModel,  
            attributes: ['username']
        }]
    }).then(result=>{
        return res.send({
            code:0,
            msg:"操作成功",
            data:result
        })
    })

    
}